package com.lesserpand.springcloudalibabacore.config.securityoauth.service.impl;

import com.lesserpand.springcloudalibabacore.config.securityoauth.dao.OauthUserDAO;
import com.lesserpand.springcloudalibabacore.config.securityoauth.entity.UserDO;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author bailuo
 * @version 2.0
 * @Description security 登录认证
 * @Date Created in 2018/12/7 9:42
 */
@Service(value = "userService")
public class UserServiceImpl implements UserDetailsService {

    @Resource
    private OauthUserDAO oauthUserDAO;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDO user = oauthUserDAO.getByUserName(username);
        if (user == null) {
            throw new UsernameNotFoundException("Invalid username or password.");
        }
        return new org.springframework.security.core.userdetails.User(user.getUserName(), user.getPassWord(), getAuthority());
    }

    private List getAuthority() {
        return Arrays.asList(new SimpleGrantedAuthority("ROLE_ADMIN"));
    }

}
